package com.example.volunteer.controller;


import com.example.volunteer.model.Points;
import com.example.volunteer.service.PointsService;
import com.example.volunteer.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/points")
public class PointsController {

    @Autowired
    private PointsService pointsService;
    // 分页查询端点
    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping
    public ResponseEntity<PageResult<Points>> getAllPoints(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(pointsService.findPointsWithPage(pageNum, pageSize));
    }

    @GetMapping("/volunteer")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Points>> getPointsByVolunteer(
            @RequestParam String volunteerId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(pointsService.findByVolunteerIdWithPage(volunteerId, pageNum, pageSize));
    }

    @GetMapping("/activity")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Points>> getPointsByActivity(
            @RequestParam Integer activityId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(pointsService.findByActivityIdWithPage(activityId, pageNum, pageSize));
    }

    @GetMapping("/points-id")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Points>> getPointsByPointsId(
            @RequestParam Integer pointsId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(pointsService.findByPointsIdWithPage(pointsId, pageNum, pageSize));
    }

    @GetMapping("/points-earned")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Points>> getPointsByPointsEarned(
            @RequestParam Integer pointsEarned,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(pointsService.findByPointsEarnWithPage(pointsEarned, pageNum, pageSize));
    }

    @GetMapping("/top10")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Points>> getTop10Volunteers(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(pointsService.findTop10VolunteersWithPage(pageNum, pageSize));
    }

    @GetMapping("/top10/period")
    @PreAuthorize("hasAnyRole('ADMIN','VOLUNTEER')")
    public ResponseEntity<PageResult<Points>> getTop10VolunteersByPeriod(
            @RequestParam int months,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(pointsService.findTop10VolunteersByPeriodWithPage(months, pageNum, pageSize));
    }

}